﻿using System;
using System.Data.Common;
using System.Data.Odbc;
using System.Text;

namespace CommonUtils
{
    /// <summary>
    /// ADO.NET for Odbc
    /// </summary>
    public class DbOdbc : DbBase
    {
        /// <summary>
        /// 打印所有驱动
        /// </summary>
        public static void PrintDrivers()
        {
            var info = new StringBuilder();
            info.AppendLine("-----------------------------WOW6432Node-------------------------");
            info.AppendLines(RegistryUtil.GetItemNames(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\ODBC\ODBCINST.INI\ODBC Drivers"));
            info.AppendLine();
            info.AppendLine("-----------------------------DEFAULT-------------------------");
            info.AppendLines(RegistryUtil.GetItemNames(@"HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"));
            info.RemoveEnd();
            info.Print();
        }

        /// <summary>
        /// 连接字符串
        /// vale/path兼容空格，不要用""''
        /// </summary>
        public class ConnectStrings
        {
            /// <summary>
            /// 通过名字获取连接字符串
            /// <para>如果使用Paradox，需要安装DB Commander 2000 RPO后驱动才能使用</para>
            /// </summary>
            /// <param name="name">系统中配置过的ODBC</param>
            [Obsolete("ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序")]
            public static string DsnName(string name)
            => string.Format("Dsn={0};", name);

            /*
            * Driver名必须与控制面板ODBC中的完全一致，尤其是空格
            * 比如 Driver={Microsoft Access Driver (*.mdb, *.accdb) 【, *】中有空格
            * 比如 Microsoft Paradox Driver (*.db ) 【(*.db )】中有空格
            * 注册表HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers中可复制具体名称
            */

            /// <summary>
            /// 通过驱动名和文件路径获取连接字符串
            /// 管理工具-ODBC数据源-查看是32位还是64位,启动项必须设置为32/64
            /// </summary>
            /// <param name="driver">驱动名</param>
            /// <param name="path">文件(夹)路径</param>
            public static string Diver(string driver, string path)
            => "Driver={" + driver + "};Dbq=" + path + ";";

            /// <summary>
            /// *.xls, *.xlsx, *.xlsm, *.xlsb
            /// Microsoft Access Database Engine Required
            /// </summary>
            public static string MicrosoftExcelDriver(string path)
            => Diver("Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)", path);

            /// <summary>
            /// *.mdb, *.accdb
            /// Microsoft Access Database Engine Required
            /// </summary>
            public static string MicrosoftAccessDriver(string path)
            => Diver("Microsoft Access Driver (*.mdb, *.accdb)", path);

            /// <summary>
            /// *.db folder不要以斜杠结尾
            /// Windows默认集成 但只支持32位模式，目标平台需切换为x86，调试需安装32位sdk，运行需要32位runtime
            /// driver模式不好用 改用DsnName模式 放弃吧 只能取TableNames 取不到数据
            /// 在 ODBC新建 中可以查看引擎版本
            /// 好像安装了DB Commander 2000 RPO后就正常了
            /// </summary>
            public static string MicrosoftParadoxDriver(string folder)
            => Diver("Microsoft Paradox Driver (*.db )", folder);
        }

        public DbOdbc(string connectString)
        : base(connectString) { }

        protected override DbConnection GetConnection()
        => new OdbcConnection(ConnectString);

        protected override DbParameter GetParameter(string key, object value)
        => new OdbcParameter(key, value);

        [Obsolete]
        protected override DbDataAdapter GetDataAdapter(DbCommand cmd)
        => new OdbcDataAdapter((OdbcCommand)cmd);
    }
}
